<template>
	<view>
		<u-form :model="formData" ref="uForm">
			<view class="form">
				<u-form-item :border-bottom="false">
					<u-input border height="80" v-model="formData.account" placeholder="请输入您的账号" />
				</u-form-item>
				<u-form-item :border-bottom="false">
					<u-input
						border
						height="80"
						type="password"
						v-model="formData.password"
						placeholder="请输入您的密码"
					/>
				</u-form-item>
			</view>
		</u-form>
		<view class=" text-r text-main font" @tap="hanglgforgotPasswordInFn">忘记密码</view>
		<view class=" mb-3 text-l">
			<u-checkbox v-model="checked" shape="circle" active-color="#0052d9">
				我已阅读
				<text class="text-primary " @click.stop="hanglgUserManualFn">《用户手册》</text>
				及
				<text class="text-primary" @click.stop="hanglgPrivacyPolicyFn">《隐私政策》</text>
			</u-checkbox>
		</view>
		<view class="mb-2"><button class="primaryBnt" @click="hanglgLoginFn">登 录</button></view>
	</view>
</template>

<script>
import { accountPwdLogin, getByUserId, getLoginAfterStatus } from '@/api/apis.js';
import { encrypt } from '@/utils/encrypFile';
export default {
	data() {
		return {
			checked: false,
			formData: {
				account: '',
				password: ''
			}
		};
	},
	methods: {
		//登录
		hanglgLoginFn() {
			if (this.validRuleFn()) {
				accountPwdLogin({
					encryptAccountAndPasswordStr: encrypt(
						`${this.$u.trim(this.formData.account)}-${this.$u.trim(this.formData.password)}`
					)
				}).then(res => {
					if (res.code == 10000) {
						uni.setStorage({
							key: 'ims-token',
							data: res.data.token
						});
						this.getByUserFn(res.data.userId);
					} else {
						this.$u.toast(res.msg);
					}
				});
			}
		},
		//获取用户个人信息
		getByUserFn(userId) {
			getByUserId({
				userId: userId
			}).then(res => {
				if (res.code == 10000) {
					uni.setStorage({
						key: 'userInfo',
						data: res.data
					});
					this.getLoginAfterStatusFn();
				} else {
					this.$u.toast(res.msg);
				}
			});
		},

		//登陆后获取状态
		getLoginAfterStatusFn() {
			getLoginAfterStatus({}).then(res => {
				if (res.code == 10000) {
					this.$Router.replaceAll({
						path: '/pages/home/index'
					});
				} else {
					this.$u.toast(res.msg);
				}
			});
		},

		//校验
		validRuleFn() {
			if (this.formData.account == '') {
				this.$u.toast('请输入账号');
				return false;
			}
			if (this.formData.password == '') {
				this.$u.toast('请输入密码');
				return false;
			}
			if (!this.checked) {
				this.$u.toast('请阅读隐私政策');
				return false;
			}
			return true;
		},
		//用户手册
		hanglgUserManualFn() {
			console.log('点击了用户手册');
		},
		//隐私政策
		hanglgPrivacyPolicyFn() {
			console.log('点击了隐私政策');
		},
		//注册账号
		hanglgsignInFn() {
			this.$Router.push({
				path: '/userPages/pages/register/index'
			});
		},
		//
		//忘记密码
		hanglgforgotPasswordInFn() {
			this.$Router.push({
				path: '/userPages/pages/forgotPassword/index'
			});
		}
	}
};
</script>

<style></style>
